﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace RedSand.Site
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            //custom
            routes.MapRoute(
               "category manager , by language",
               "{language}/category/manager/{type}",
               new { controller = "category", action = "manager", type = UrlParameter.Optional },
               new { language = @"\w{2}" }
            );
            routes.MapRoute(
               "category create, by language",
               "{language}/category/create/{type}/{count}",
               new { controller = "category", action = "create", type = UrlParameter.Optional, count = UrlParameter.Optional },
               new { language = @"\w{2}" }
            );
            routes.MapRoute(
               "category edit, by language",
               "{language}/category/edit/{type}/{id}/{count}",
               new { controller = "category", action = "edit", id = UrlParameter.Optional, count = UrlParameter.Optional },
               new { language = @"\w{2}" }
            );
            routes.MapRoute(
               "category delete, by language",
               "{language}/category/delete/{type}/{id}",
               new { controller = "category", action = "delete", id = UrlParameter.Optional, type = UrlParameter.Optional },
               new { language = @"\w{2}" }
            );

            // global with language
            routes.MapRoute(
              "Details Support, language",
              "{language}/support/details/{SkypeId}",
               new { controller = "Support", action = "GetBySkypeId", SkypeId = UrlParameter.Optional },
               new { language = @"\w{2}" }
          );
            routes.MapRoute(
               "Faq, page, language",
               "{language}/Faq/ListFaq/{page}",
               new { controller = "Faq", action = "ListFaq", page = UrlParameter.Optional },
               new { language = @"\w{2}" }
           );

            routes.MapRoute(
                "by keyword, page, language",
                "{language}/login/{ReuturnUrl}",
                new { controller = "Home", action = "login", ReturnUrl = UrlParameter.Optional },
                new { language = @"\w{2}" }
            );
            routes.MapRoute(
                "register , by language",
                "{language}/register/{ReuturnUrl}",
                new { controller = "Home", action = "register", ReuturnUrl = UrlParameter.Optional },
                new { language = @"\w{2}" }
            );

            routes.MapRoute(
                "by keyword, page , by language",
                "{language}/{controller}/search/{keyword}/{page}",
                new { controller = "news", action = "search", page = UrlParameter.Optional, keyword = UrlParameter.Optional },
                new { language = @"\w{2}" }
            );
            routes.MapRoute(
                "by category, page, language",
                "{language}/{controller}/category/{CategoryId}/{page}",
                new { controller = "news", action = "search", page = UrlParameter.Optional, CategoryId = UrlParameter.Optional },
                new { language = @"\w{2}" }
            );
            routes.MapRoute(
                "by type, language",
                "{Language}/{Controller}/type/{type}",
                new { Controller = "news", action = "GetListCategory", type = UrlParameter.Optional },
                new { language = @"\w{2}" }
            );

            routes.MapRoute(
                "index page, by language",
                "{Language}/{Controller}/{page}",
                new { controller = "news", action = "index", page = UrlParameter.Optional },
                new { controller = @"news|product|schedule", page = @"\d+|pageindex", language = @"\w{2}" }
            );
            routes.MapRoute(
                "by id, title, language",
                "{language}/{controller}/{action}/{id}/{title}",
                new { controller = "home", action = "index", id = UrlParameter.Optional, title = UrlParameter.Optional },
                new { language = @"\w{2}" }
            );
            routes.MapRoute(
                "by id, language",
                "{language}/{controller}/{action}/{id}",
                new { controller = "home", action = "index", id = UrlParameter.Optional },
                new { language = @"\w{2}" }
            );

            //custom
            routes.MapRoute(
               "category manager",
               "category/manager/{type}",
               new { controller = "category", action = "manager", type = UrlParameter.Optional }
            );
            routes.MapRoute(
               "category create",
               "category/create/{type}/{count}",
               new { controller = "category", action = "create", type = UrlParameter.Optional, count = UrlParameter.Optional }
            );
            routes.MapRoute(
               "category edit",
               "category/edit/{type}/{id}/{count}",
               new { controller = "category", action = "edit", id = UrlParameter.Optional, count = UrlParameter.Optional }
            );
            routes.MapRoute(
               "category delete",
                //"category/delete/{id}/{type}",
               "category/delete/{type}/{id}",
               new { controller = "category", action = "delete", id = UrlParameter.Optional, type = UrlParameter.Optional }
            );

            //global
            routes.MapRoute(
               "Details Support",
               "support/details/{SkypeId}",
               new { controller = "Support", action = "GetBySkypeId", SkypeId = UrlParameter.Optional }
           );
            routes.MapRoute(
               "Faq, page",
               "Faq/ListFaq/{page}",
               new { controller = "Faq", action = "ListFaq", page = UrlParameter.Optional }
           );
            routes.MapRoute(
                "by keyword, page",
                "{controller}/search/{keyword}/{page}",
                new { controller = "news", action = "search", page = UrlParameter.Optional, keyword = UrlParameter.Optional }
            );
            routes.MapRoute(
                "by category, page",
                "{Controller}/category/{CategoryId}/{page}",
                new { controller = "news", action = "search", CategoryId = UrlParameter.Optional, page = UrlParameter.Optional }
            );

            routes.MapRoute(
                "by type",
                "{Controller}/type/{type}",
                new { Controller = "news", action = "GetListCategory", type = UrlParameter.Optional }
            );

            routes.MapRoute(
                "by index page",
                "{Controller}/{page}",
                new { controller = "news", action = "index", page = UrlParameter.Optional },
                new { controller = @"news|product|schedule", page = @"\d+|pageindex" }
            );

            routes.MapRoute(
                "by id title",
                "{controller}/{action}/{id}/{title}",
                new { controller = "home", action = "index", id = UrlParameter.Optional, title = UrlParameter.Optional }
            );
            routes.MapRoute(
                "by id",
                "{controller}/{action}/{id}",
                new { controller = "home", action = "index", id = UrlParameter.Optional }
            );
        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            //add json value provider
            //ValueProviderFactories.Factories.Add(new JsonValueProviderFactory());
        }
    }
}